<html>
<head><meta charset="utf-8"><title>pointer width vs pointer size vs usize · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html">pointer width vs pointer size vs usize</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="163081699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163081699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicholas Sim <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163081699">(Apr 11 2019 at 08:49)</a>:</h4>
<p>I'm attempting to port Rust to a platform (CHERI) which has <code>target_pointer_width = 128</code>, but <code>usize</code> should be <code>u64</code>: the remaining bits are used for capability tagging. It seems that it's quite hard to get a 64-bit <code>usize</code>,  as it seems to depend on <code>pointer_size</code> from the data layout. However, it seems that <code>pointer_size</code> is also used for offsets.</p>
<p>I wonder if I'm doing something silly, or if the meaning has been overloaded?<br>
My understanding is that there is no support (currently!) for architectures where pointer width, pointer size, or usize can differ. Is this correct? Or maybe this should "just work" -- I've configured it wrongly?</p>
<p>Happy if anyone can shed light on this!</p>



<a name="163081968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163081968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Zoxc <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163081968">(Apr 11 2019 at 08:53)</a>:</h4>
<p><code>usize</code> is the size of a pointer by definition. So it should be <code>u128</code> then.</p>



<a name="163082161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163082161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163082161">(Apr 11 2019 at 08:56)</a>:</h4>
<p>Not only is it defined in Rust that <code>usize</code> is the same size as <code>*const ()</code>, but I also believe the compiler can't cope with pointer sizes other than <code>32</code> and <code>64</code> bit with some minor support for <code>16</code> bit</p>



<a name="163084222"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163084222" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicholas Sim <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163084222">(Apr 11 2019 at 09:27)</a>:</h4>
<p>@Zoxc do you mean <code>usize == uintptr_t</code>, rather than <code>size_t</code>? And since <code>usize</code> is implicitly used for <code>size_t</code>, there is an (assumed) equivalence between the three?</p>
<p>As for coping with other pointer sizes -- I've seen the (16-bit) AVR patches, and I can attest that rustc "copes" with 128-bit <code>target_pointer_width</code>, <code>pointer_size</code>, and <code>usize</code>. The trouble comes when you can't call <code>ptrtoint</code> to the pointer size, or certain GEP indexing operations, etc., where 128-bit operands are not accepted. To an extent, I've solved this using <code>intcast</code>s -- this breaks when with <code>opt-level &gt;= 2</code></p>



<a name="163103879"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163103879" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163103879">(Apr 11 2019 at 14:22)</a>:</h4>
<p>Traditionally, <code>usize == uintptr_t</code>, yes. I can't speak to the rest necessarily.</p>



<a name="163104472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163104472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163104472">(Apr 11 2019 at 14:28)</a>:</h4>
<p>const eval will fail guaranteed if you use <code>usize</code> values bigger than <code>u64::max_value</code></p>



<a name="163104529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163104529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163104529">(Apr 11 2019 at 14:29)</a>:</h4>
<p>const eval has a bug ;)</p>



<a name="163104869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163104869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163104869">(Apr 11 2019 at 14:32)</a>:</h4>
<p>well... fixing it will regress max-rss even for all hosts</p>



<a name="163104957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163104957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163104957">(Apr 11 2019 at 14:33)</a>:</h4>
<p>because all hosts pay the overhead of whatever the maximum ptr width is?</p>



<a name="163105100"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105100" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105100">(Apr 11 2019 at 14:35)</a>:</h4>
<p>Might make sense to go to flexible-width integers for all sorts of integral types.</p>



<a name="163105562"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105562" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105562">(Apr 11 2019 at 14:40)</a>:</h4>
<blockquote>
<p>because all hosts pay the overhead of whatever the maximum ptr width is?</p>
</blockquote>
<p>yes</p>



<a name="163105672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105672">(Apr 11 2019 at 14:40)</a>:</h4>
<blockquote>
<p>Might make sense to go to flexible-width integers for all sorts of integral types.</p>
</blockquote>
<p>then we'd end up with a pointer to the heap alloc, which is also bigger than 64 bits ;)</p>



<a name="163105687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105687">(Apr 11 2019 at 14:41)</a>:</h4>
<p>(in total, pointer + data)</p>



<a name="163105738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105738">(Apr 11 2019 at 14:41)</a>:</h4>
<p>Not necessarily.</p>



<a name="163105828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105828">(Apr 11 2019 at 14:42)</a>:</h4>
<p>anything 63-bits long can be represented as a regular tagged integer, anything above as a pointer to integers</p>



<a name="163105832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105832">(Apr 11 2019 at 14:42)</a>:</h4>
<p>so only heapify if the value is bigger than <code>usize</code> and use some bits to clarify that?</p>



<a name="163105852"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105852" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105852">(Apr 11 2019 at 14:42)</a>:</h4>
<p>yeah, that’s one of the options</p>



<a name="163105935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/pointer%20width%20vs%20pointer%20size%20vs%20usize/near/163105935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/pointer.20width.20vs.20pointer.20size.20vs.20usize.html#163105935">(Apr 11 2019 at 14:43)</a>:</h4>
<p>another option is to use indices into some sort of an array, and those can be made smaller -- only as large as necessary to store the necessary number of integers, rather than address the whole address space.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>